{% load static %}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link href="{% static 'layui/css/layui.css' %}" rel="stylesheet" media="all">
</head>
<body>
<style>
    .demo-login-container {
        margin: 21px auto 0;
    }

    .demo-login-other .layui-icon {
        position: relative;
        display: inline-block;
        margin: 0 2px;
        top: 2px;
        font-size: 26px;
    }

    #bg {
        width: 24%;
        height: 500px;
        margin: 100px auto;
        border-color: black;
        border-width: 2px;
        border-radius: 15px;
        border: 1px solid #000000;
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        padding: 10px;
        text-align: center;
        line-height: 80px;

    }
</style>
<div id="bg">
    <h1 style="text-align: center">登录</h1>
    <form class="layui-form">
        <div class="demo-login-container">
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-username"></i>
                    </div>
                    <input type="text" name="username" value="" lay-verify="required" placeholder="用户名"
                           lay-reqtext="请填写用户名" autocomplete="off" class="layui-input" lay-affix="clear">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-password"></i>
                    </div>
                    <input type="password" name="password" value="" lay-verify="required" placeholder="密   码"
                           lay-reqtext="请填写密码" autocomplete="off" class="layui-input" lay-affix="eye">
                </div>
            </div>
            <div class="layui-form-item" style="text-align: left">
                <div class="layui-input-wrap">
                    <input type="radio" name="role" value="admin" title="管理员">
                    <input type="radio" name="role" value="user" title="用户" checked>
                </div>
            </div>

            <a href="{% url 'login:注册' %}?id=" style="line-height: 20px">无账号密码,点击这里注册</a>
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="demo-login">登录</button>
            </div>
        </div>
    </form>
</div>


<!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
<script src="{% static 'layui/layui.js' %}"></script>
<script>
    layui.use(function () {
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;
        // 提交事件
        form.on('submit(demo-login)', function (data) {
            var field = data.field; // 获取表单字段值

            // 此处可执行 Ajax 等操作
            $.ajax(
                {
                    url: '{% url 'login:登录' %}',
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify(field),
                    success: function (data) {
                        if (data === '200') {
                            window.location.href = '{% url 'content:首页' %}';
                        } else if (data === '999') {
                            window.location.href = '{% url 'manager:我的' %}';
                        } else {
                            layer.msg('用户名或密码错误', {icon: 5});
                        }

                    },
                    error: function () {
                        window.alert('请求失败,请联系系统管理员');
                    }

                })

            return false; // 阻止默认 form 跳转
        });
    });
</script>

</body>
</html>